#include "Cal.hpp"
#include "Protocol.hpp"
#include "TcpServer.hpp"
#include "Daemon.hpp"
#include <memory>


void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " port" << std::endl;
}

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }
    std::cout << "守护进程启动！" << std::endl;

    Daemon(1, 0);
    //daemon(0, 0);

    Enable_File_Log_Strategy();

    //顶层
    std::unique_ptr<Cal> cal = std::make_unique<Cal>();

    //协议层
    std::unique_ptr<Protocol> protocol = std::make_unique<Protocol>([&cal](Request& req){
        return cal->Execute(req);
    });

    //服务器
    std::shared_ptr<TcpServer> tsvr = std::make_shared<TcpServer>(std::stoi(argv[1]), [&protocol](std::shared_ptr<Socket> &sock, InetAddr &client){
        protocol->GetRequest(sock, client);
    });

    tsvr->Start();
}